package com.smyh.protcol.host;

import java.util.ArrayList;
import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;

import com.smyh.bean.CmdDataModel;
import com.smyh.commcache.commd.CommandCache;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.BytesHex;

public class ProtocolCommMethod {

	/**
	 * 发送主机同步指令
	 */
	public static void sendHostSyncCMD(){
		String linkStr = HostCMDUtil.linkAddrStr;
		
		byte[] linkStrBytes = BytesHex.hexStringToBytes(linkStr);
		IoBuffer buf = HostCMDUtil.syncHostCMD(linkStrBytes);
		
		CmdDataModel cmdObj = new CmdDataModel();
		
		cmdObj.setGoalAddr(linkStr);
		cmdObj.setCounts((byte) 0);
		cmdObj.setSendTime(System.currentTimeMillis() -5000);
		List<IoBuffer> bufs = new ArrayList<IoBuffer>();
		bufs.add(buf);
		cmdObj.setContentsList(bufs);
		
		CommandCache.commandMap.put(CommandCache.HOST_SYNC_CMD, cmdObj);
	}
	/**
	 * 发送主机升级0-9（主机、指示器）指令
	 */
	public static void sendHostUpdateProgramCmd(byte deviceIndex){
		String linkStr = HostCMDUtil.linkAddrStr;
		
		IoBuffer buf = CMDUtil.getUpdateForCMD(deviceIndex);
		
		CmdDataModel cmdObj = new CmdDataModel();
		
		cmdObj.setGoalAddr(linkStr);
		cmdObj.setCounts((byte) 0);
		cmdObj.setSendTime(System.currentTimeMillis() -5000);
		List<IoBuffer> bufs = new ArrayList<IoBuffer>();
		bufs.add(buf);
		cmdObj.setContentsList(bufs);
		
		CommandCache.commandMap.put(CommandCache.HOST_UPGRADE_CMD, cmdObj);
	}

	/**
	 * 遥控指示器翻牌
	 */
	public static void controlTurnCup(byte group,byte phase,byte value){
		String linkStr = HostCMDUtil.linkAddrStr;
		byte[] pointBytes = new byte[2];
		
		switch (group) {
		case 1:{
			//1组
			switch (phase) {
			case 1:{
				//A相
				pointBytes[0] = 0x1;
				pointBytes[1] = 0x60;
				break;
			}
				
			case 2:{
				//B相
				pointBytes[0] = 0x2;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			case 3:{
				//C相
				pointBytes[0] = 0x3;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			default:
				break;
			}
			break;
		}
			
		case 2:{
			//2组
			switch (phase) {
			case 1:{
				//A相
				pointBytes[0] = 0xa;
				pointBytes[1] = 0x60;

				break;
			}
				
			case 2:{
				//B相
				pointBytes[0] = 0xb;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			case 3:{
				//C相
				pointBytes[0] = 0xc;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			default:
				break;
			}
			break;
		}
			
		case 3:{
			//3组
			switch (phase) {
			case 1:{
				//A相
				pointBytes[0] = 0x13;
				pointBytes[1] = 0x60;

				break;
			}
				
			case 2:{
				//B相
				pointBytes[0] = 0x14;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			case 3:{
				//C相
				pointBytes[0] = 0x15;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			default:
				break;
			}
			break;
		}
			
		default:
			break;
		}
		
		IoBuffer buf = HostCMDUtil.controlDeviceCMD(pointBytes, value);
		
		CmdDataModel cmdObj = new CmdDataModel();
		
		cmdObj.setGoalAddr(linkStr);
		cmdObj.setCounts((byte) 0);
		cmdObj.setSendTime(System.currentTimeMillis() -5000);
		List<IoBuffer> bufs = new ArrayList<IoBuffer>();
		bufs.add(buf);
		cmdObj.setContentsList(bufs);
		/**
		 * 转碗控制指令
		 */
		CommandCache.commandMap.put(CommandCache.DEVICE_CONTROL_TURN_CUP , cmdObj);
	}
	/**
	 * 遥控指示器复归
	 */
	public static void controlDeviceReset(byte group,byte phase,byte value){
		String linkStr = HostCMDUtil.linkAddrStr;
		byte[] pointBytes = new byte[2];
		
		switch (group) {
		case 1:{
			//1组
			switch (phase) {
			case 1:{
				//A相
				pointBytes[0] = 0x7;
				pointBytes[1] = 0x60;
				break;
			}
				
			case 2:{
				//B相
				pointBytes[0] = 0x8;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			case 3:{
				//C相
				pointBytes[0] = 0x9;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			default:
				break;
			}
			break;
		}
			
		case 2:{
			//2组
			switch (phase) {
			case 1:{
				//A相
				pointBytes[0] = 0x10;
				pointBytes[1] = 0x60;

				break;
			}
				
			case 2:{
				//B相
				pointBytes[0] = 0x11;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			case 3:{
				//C相
				pointBytes[0] = 0x12;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			default:
				break;
			}
			break;
		}
			
		case 3:{
			//3组
			switch (phase) {
			case 1:{
				//A相
				pointBytes[0] = 0x17;
				pointBytes[1] = 0x60;

				break;
			}
				
			case 2:{
				//B相
				pointBytes[0] = 0x18;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			case 3:{
				//C相
				pointBytes[0] = 0x19;
				pointBytes[1] = 0x60;
				
				break;
			}
				
			default:
				break;
			}
			break;
		}
			
		default:
			break;
		}
		
		IoBuffer buf = HostCMDUtil.controlDeviceCMD(pointBytes, value);
		
		CmdDataModel cmdObj = new CmdDataModel();
		List<IoBuffer> bufs = new ArrayList<IoBuffer>();
		bufs.add(buf);
		cmdObj.setContentsList(bufs);
		cmdObj.setGoalAddr(linkStr);
		cmdObj.setCounts((byte) 0);
		cmdObj.setSendTime(System.currentTimeMillis() -5000);
		
		CommandCache.commandMap.put(CommandCache.CONTROL_RESET, cmdObj);
	}
}
